package cn.ctyun.videoplayer.playerevent;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PlayerEventUtils {

    /**
     * 获取应用程序名称
     */
    public static String getAppName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getNetworkTypeString(Context context) {
        String networkType = "unknown";
        int networkTypeInt = getNetWorkType(context);
        if (1 == networkTypeInt) {
            networkType = "2g";
        } else if (2 == networkTypeInt) {
            networkType = "3g";
        } else if (3 == networkTypeInt) {
            networkType = "4g";
        } else if (0 == networkTypeInt) {
            networkType = "wifi";
        }

        return networkType;
    }

    public static int getNetWorkType(Context mContext) {
        int netWorkType = -1;
        ConnectivityManager connectivityManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null) {
            if (activeNetInfo.getType() == 1) {
                netWorkType = 0;
            } else if (activeNetInfo.getType() == 0) {
                int subType = activeNetInfo.getSubtype();
                if (subType != 1 && subType != 4 && subType != 2 && subType != 7 && subType != 11) {
                    if (subType != 8 && subType != 9 && subType != 3 && subType != 10 && subType != 5 && subType != 6 && subType != 12 && subType != 14) {
                        if (subType == 13) {
                            netWorkType = 3;
                        }
                    } else {
                        netWorkType = 2;
                    }
                } else {
                    netWorkType = 1;
                }
            }
        }

        return netWorkType;
    }

    public static String getCurrentTime() {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(dt);
    }

    /**
            * 将输入流转换成字符串
     *
             * @param is 从网络获取的输入流
     * @return
             */
    public String streamToString(InputStream is) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            baos.close();
            is.close();
            byte[] byteArray = baos.toByteArray();
            return new String(byteArray);
        } catch (Exception e) {
            return null;
        }
    }

}
